﻿@using Smartstore.Web.Models.Cart;

@model ShoppingCartModel

<div class="order-summary-content cart-content" data-empty-text="@T("ShoppingCart.CartIsEmpty")">

    <zone name="order_summary_content_before" />
    
    <partial name="_OrderReviewData" model="Model.OrderReviewData" />

    @if (Model.DisplayCommentBox && !Model.IsEditable)
    {
        <partial name="_CommentBox" model="Model" />
    }

    @if (Model.Items.Any())
    {
        <div sm-if="Model.Warnings.Any()" class="alert alert-danger">
            <ul>                
                @foreach(var warning in Model.Warnings)
                {
                    <li>@warning</li>
                }
            </ul>
        </div>

        @* "File upload" attribute control type requries "multipart/form-data" *@
        <form asp-route="ShoppingCart" method="post" enctype="multipart/form-data" onkeydown="return event.key != 'Enter';">

            <div class="card">
                <div id="cart-items" class="cart mb-0" attr-class='(Model.IsEditable, "cart-editable")'>
                    <div class="cart-head">
                        <div class="cart-row">
                            <div class="cart-col cart-col-main">
                                @T("ShoppingCart.Product(s)")
                            </div>
                            <div class="cart-col cart-col-price">
                                @T("ShoppingCart.UnitPrice")
                            </div>
                            <div class="cart-col cart-col-qty">
                                @T("ShoppingCart.Quantity")
                            </div>
                            <div class="cart-col cart-col-price cart-col-subtotal">
                                @T("ShoppingCart.ItemTotal")
                            </div>
                        </div>
                    </div>
                    <div class="cart-body">
                        <partial name="CartItems" model="Model" />
                    </div>
                </div>

                <div class="cart-footer rounded-bottom">
                    <div class="row">
                        <div class="col-md-6">
                            @if (Model.IsEditable)
                            {
                                <zone name="order_summary_actions_before" />

                                <partial name="_DiscountBox" model="Model.DiscountBox" />
                                <partial name="_GiftCardBox" model="Model.GiftCardBox" />
                                <partial name="_RewardPointsBox" model="Model.RewardPoints" />
                                <partial name="EstimateShipping" model="Model.EstimateShipping" />

                                <zone name="order_summary_actions_after" />
                            }
                            else
                            {
                                <div sm-if="Model.CheckoutAttributeInfo.HasValue()" class="selected-checkout-attributes">
                                    @Html.Raw(Model.CheckoutAttributeInfo)
                                </div>
                            }
                        </div>
                        <div class="col-md-6">
                            @{
                                if (Model.IsEditable)
                                {
                                    <partial name="_CheckoutAttributes" model="Model.CheckoutAttributes" view-data="new ViewDataDictionary(ViewData)" />
                                }

                                <zone name="order_summary_totals_before" />

                                <div id="order-totals">
                                    @await Component.InvokeAsync("OrderTotals", new { isEditable = Model.IsEditable })
                                </div>

                                <zone name="order_summary_totals_after" />
                            }
                        </div>
                    </div>
                </div>
            </div>

            @if (Model.IsEditable)
            {
                <zone name="order_summary_buttons_before" />

                <div class="cart-buttons my-4 row">
                    <div class="col-sm-6 col-lg-4 order-last order-sm-first mt-3 mt-sm-0">
                        <button class="btn btn-secondary btn-block btn-lg btn-continue-shopping" type="submit" name="continueshopping" value="continueshopping">
                            <i class="fa fa-angle-left mr-3"></i>
                            <span>@T("ShoppingCart.ContinueShopping")</span>
                        </button>
                    </div>
                    <div class="col-sm-6 col-lg-4 ml-lg-auto">
                        <div id="start-checkout-buttons">
                            <div class="checkout-buttons">
                                <input type="submit" name="startcheckout" value="startcheckout" id="startcheckout" class="d-none" />
                                <button type="button" id="checkout" name="checkout"
                                        class="btn btn-danger btn-lg btn-block btn-checkout"
                                        onclick="$('#startcheckout').trigger('click'); return false;">
                                    <span>@T("Checkout.Button")</span>
                                    <i class="fa fa-angle-right ml-3"></i>
                                </button>
                            </div>

                            @{
                                var paymentButtons = (await Model.ButtonPaymentMethods.Items
                                    .SelectAwait(x => x.InvokeAsync(ViewContext))
                                    .AsyncToList())
                                    .Where(x => x.HasContent());
                            }

                            <div sm-if="paymentButtons.Any()" class="addon-buttons">
                                <div class="d-flex justify-content-sm-end justify-content-center flex-wrap">
                                    <div class="mt-3 mb-1 d-block col-12 text-center text-sm-right">
                                        <span>@T("ShoppingCart.AlternativePaymentButtons")</span>
                                    </div>

                                    @*Payment method buttons (e.g. Paypal, Amazon Pay)*@
                                    @foreach (var button in paymentButtons)
                                    {
                                        @button
                                    }
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <zone name="order_summary_buttons_after" />
            }

            <zone name="order_summary_content_after" />
        </form>

        if (Model.IsEditable)
        {
            @await Component.InvokeAsync("CrossSellProducts")
        }
    }
    else
    {
        <div class="alert alert-warning fade show">
            @T("ShoppingCart.CartIsEmpty")
        </div>
    }
</div>

@if (!Model.IsEditable && Model.Items.Any(x => x.IsDownload && x.HasUserAgreement))
{
    <modal id="user-agreement-modal" sm-render-at-page-end="true" sm-center-vertically="true" sm-size="FlexSmall">
        <modal-body></modal-body>
        <modal-footer>
            <button class="btn btn-secondary" data-dismiss="modal">@T("Common.Confirm")</button>
        </modal-footer>
    </modal>

    <script sm-target-zone="scripts" data-origin="order-summary">
        $(function () {
            $('a.download-user-agreement').on("click", function () {
                var url = '@(Url.Action("GetUserAgreement", "Download"))?productId=' + $(this).attr('data-id'),
                    dialog = $('#user-agreement-modal');

                dialog.find('.modal-body').empty();

                $.get(url, function (html) {
                    dialog.find('.modal-body').html(html);
                });

                dialog.modal();

                return false;
            });
        });
    </script>
}

@if (Model.IsEditable)
{
    <script src="~/js/public.refreshcart.js" sm-target-zone="scripts"></script>
}